const app = getApp();
Page({
    data:{
        company:"",
        name:"",
        region:[{code:0,name:'省'},{code:0,name:'市'},{code:0,name:'区'}],
        mobile:'',
        code:'',
        send:false,
        alreadySend: false,
        second:60,
        other:'',
        user:''
    },
    inputCode(e){
        let code = e.detail.value
        if (code.length === 6){
            this.setData({
                code:e.detail.value
            })
            this.activeButton()
        }
    },
    inputUser(e){
        this.setData({
            user:e.detail.value
        })
        this.activeButton()
    },
    inputCompany(e){
        this.setData({
            company:e.detail.value
        })
        this.activeButton()
    },
    inputName(e){
        this.setData({
            name:e.detail.value
        })
        this.activeButton()
    },
    inputOther(e) {
        let other = e.detail.value
        this.setData({
            other:other
        })
        this.activeButton()
    },

    inputPhoneNum(e){
        let phoneNum = e.detail.value
        if (phoneNum.length === 11) {
            let checkedNum = this.checkPhoneNum(phoneNum)
            if (checkedNum) {
                this.setData({
                    mobile: phoneNum
                })
                console.log('phoneNum' + this.data.mobile)
                this.showSendMsg()
                this.activeButton()
            }
        } else {
            this.setData({
                mobile: ''
            })
            this.hideSendMsg()
        }
    },
    hideSendMsg: function () {
        this.setData({
            send: false,
            disabled: true,
            buttonType: false
        })
    },
    checkPhoneNum: function (phoneNum) {
        let str = /^1\d{10}$/
        if (str.test(phoneNum)) {
            return true
        } else {
            wx.showToast({
                title: '手机号不正确',
                image: '../../images/icon/fail.jpg'
            })
            return false
        }
    },
    showSendMsg: function () {
        if (!this.data.alreadySend) {
            this.setData({
                send: true
            })
        }
    },
    sendMsg: function () {
        if (this.data.send){
            wx.request({
                url:app.globalData.msg,
                method:'POST',
                dataType: 'json',
                data:{mobile:this.data.mobile},
                success:response=>{
                    if (response.statusCode===200 && response.data.success){
                        this.setData({
                            alreadySend: true,
                            send: false
                        })
                        this.timer()
                    }else {
                        wx.showToast({
                            title: '手机号不正确',
                            icon:'fail'
                        })
                    }
                }
            })

        }

    },
    timer: function () {
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    this.setData({
                        second: this.data.second - 1
                    })
                    if (this.data.second <= 0) {
                        this.setData({
                            second: 60,
                            alreadySend: false,
                            send: true
                        })
                        resolve(setTimer)
                    }
                }
                , 1000)
        })
        promise.then((setTimer) => {
            clearInterval(setTimer)
        })
    },
    activeButton(){
        let {mobile, address, face,birthday,real_name,card,gender,nation,back,code} = this.data
        if (mobile && address && face && birthday && real_name && card && gender && nation && back && code ) {
            this.setData({
                buttonType: true
            })
        } else {
            this.setData({
                buttonType: false
            })
        }
    },
    bindCityChange:function(e){
        //
        // let data = this.cityCode(e.detail);
        // console.log(data);
        this.setData({
            region:this.cityCode(e.detail)
        })
    },
    cityCode(obj){
        let codeArray = obj.code
        let valueArray = obj.value
        let data = [];
        for (let i=0;i<3;i++){
            let obj = {}
            obj.code = codeArray[i];
            obj.name = valueArray[i];
            data.push(obj)
        }
        return data;
    },
    submit(){
        let { company, name, region, mobile, code, other, user } = this.data
        if (company && name && region && mobile && code && other && user){
            app.axios('/register','POST',response=>{
                // if (response){
                    wx.navigateTo({
                        url:'/pages/regSuccess/index'
                    })
                // }
            },{
                gname:company,
                name:name,
                area:region,
                x_desc:other,
                x_name:user,
                phone:mobile,
                code:code
            })
        }
    }
});
